package org.vectrics.web.struts;

import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.A;
import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPerson;
import org.vectrics.company.CompanyPersonCriteria;
import org.vectrics.company.CompanyPersonPageList;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;
import org.vectrics.struts.Request;

public class CompanyPersonListTag extends PageListTag {
	public static final long serialVersionUID = 2435678043534L;

	public final static String TRUE = "true";
	public final static String FALSE = "false";
	
	private static Category log = Category.getInstance(CompanyListTag.class);
    public String PAGE_KEY_PARAM = "page_key";
    private String companyId = null;
    private String personId = null;
    private String showInactive = "false";
    
    private Company company = null;
    
    public CompanyPersonListTag() {
    	super();
    }
    
    
    public String getPageListLink() {
        return("/admin/company/persons/view.do?companyId=" + this.companyId);
    }
    
    public String getNoElementsMessage() {
        return(this.getPhrase("phrase.no.company.persons"));
    }
    
    /**
     * @return Returns the companyId.
     */
    public String getCompanyId() {
        return companyId;
    }
    /**
     * @param companyId The companyId to set.
     */
    public void setCompanyId(String companyId) {
        this.companyId = companyId;
    }
    
    public Company getCompany() {
        if (company == null) {
            company = CoreServiceLocator.getCompanyService().findCompany(new Long(companyId));
        }
        return(company);
    }
    
    public String getDescription() {
        log.debug("getDescription() called.");
        return(null);
    }

    
    protected PageList createPageList() {
    	CompanyPersonCriteria criteria = new CompanyPersonCriteria();
    	if (companyId != null)
    		criteria.setCompany(CoreServiceLocator.getCompanyService().findCompany(new Long(companyId)));
    	if (personId != null)
    		criteria.setPerson(CoreServiceLocator.getPersonService().findPersonById(new Long(personId)));
    	PageList pageList = new CompanyPersonPageList(criteria);
    	return(pageList);
    }
    
    
    protected void makeHeaderCells(Row row) {
        log.debug("makeHeaderCells() called.");
        
        Cell cell = row.addCell();
        //cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.person.name")));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("phrase.person.title")));

        cell = row.addCell();
		//cell.setClass("tableHdrBg");
        cell.addElement(TextHelper.getFont("tableHdrFont", "Options"));
    }

    /*
     * 
     */
    protected void makeObjectRow(Request request, Row row, CompanyPerson companyPerson) throws DataException {
    	log.debug("makeObjectRow()");
        Cell cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", companyPerson.getPerson().getName()));

        cell = row.addCell();
        cell.addElement(TextHelper.getFont("subFont", companyPerson.getTitle()));

        cell = row.addCell();
        A link = new A();
        link.setHref(request.getContextPath() + "/admin/company/person/view.do" 
        		+ "?companyPersonId=" + companyPerson.getId()
        		+ "&companyId=" + companyPerson.getId());
        link.setClass("subLink");
        link.addElement(this.getPhrase("phrase.view"));
        cell.addElement(link);
    }

    protected void setTableProperties(BorderTable table) {
        table.setWidth(500);
    }
    
    
    protected void showList(String description, Cell mainCell, List companyPersonList, Form form)
    {
        log.debug("showList() called.");
        
        Iterator companyIter = companyPersonList.iterator();

        BorderTable table = new BorderTable();
        
        table.setID("border_table");
        table.setWidth(600);
        mainCell.addElement(table);
        
        
        if (companyIter.hasNext()) {
            Row row = table.addRow();
            row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
            
            makeHeaderCells(row);
            while (companyIter.hasNext()) {
				CompanyPerson companyPerson = (CompanyPerson)companyIter.next();
				try {
	                row = table.addRow();
	                makeObjectRow(this.getRequest(), row, companyPerson);
				}
				catch (Exception de) {
					log.error("Exception finding company", de);
				}

            }
        }
    }

	public String getShowInactive() {
		return showInactive;
	}

	public void setShowInactive(String showInactive) {
		this.showInactive = showInactive;
	}

	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}
}
